from django.contrib import admin

from django.dispatch import receiver
from django.db.models.signals import pre_delete
from django.utils.html import format_html

from FmyTest.models import *
import os
from django.db.models.signals import pre_save

# 必须继承ModelAdmin基类,才可以调整参数,HostDB则是你的表的名称
@admin.register(versioncontrol)
class MyAdmin(admin.ModelAdmin):
    admin.site.site_title = "云智能平台精简安装包"
    admin.site.site_header = "云智能平台精简安装包"


    # list_display = 你需要或者想要展示在页面中的字段
    list_display = ("Soft_version", "ARM64", "AMD64", "Status", 'Operate')
    # 禁止跳转链接  禁止点击页面主键进行跳转到修改页面
    list_display_links = None
    # def has_add_permission(self, request):
    #     # 禁用添加按钮
    #     return False
    # def has_delete_permission(self, request, obj=None):
    #     # 禁用删除按钮
    #     return False
    # 重写方法屏蔽按钮
    # def change_view(self, request, object_id, form_url='', extra_context=None):
    #     extra_context = extra_context or {}
    #     extra_context['show_save_and_add_another'] = True
    #     extra_context['show_save_and_continue'] = False
    #     return super(MyAdmin, self).change_view(request, object_id,
    #         form_url, extra_context=extra_context)
    def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
        context.update({
            'show_save': True,
            'show_save_and_continue': False,
            'show_save_and_add_another':False,
        })
        return super().render_change_form(request, context, add, change, form_url, obj)
    # 只读字段
    # readonly_fields = ["soft_version", "arm64", "amd64"]
    # search_fields = 添加搜索功能,并以HostName作为搜索条件
    search_fields = ("soft_version",)
    # ordering = 设置以HostAddr作为排序条件
    # ordering = ("HostAddr",)
    # list_per_page = 设置每页显示的字段数
    list_per_page = 10
    # https: // zhuanlan.zhihu.com / p / 113447102?tt_from = weixin & utm_id = 0
    # fieldsets = [
    #     (None, {
    #         'fields': [''
    #             'soft_version',
    #             # 'arm64',
    #             'amd64',
    #             'stats'
    #         ],
    #     }),
    # ]
    # save_as_continue = False # 修改完成之后跳转到元素列表页面
    # ordering 是根据什么来排序
    # @admin.display(description='状态',ordering='stats')
    @admin.display(description='状态')
    def Status(self,obj):
        if obj.stats == 's':
            format_td = format_html('''
            <span style="padding: 2px 8px;
            font-size: 0.7rem;
            background-color: #00ff009c;
            letter-spacing: 1px;
            color: white;
            border-radius: 2px;">启用</span>
            ''')
        elif obj.stats == 'f':
            format_td = format_html('''
            <span style="padding:2px 8px;
            font-size: 0.7rem;
            background-color:#FF6770;
            letter-spacing: 1px;
            color:white;
            border-radius: 2px;">禁用</span>
            ''')
        return format_td

    @admin.display(description='软件版本')
    def Soft_version(self,obj):
        return obj.soft_version

    @admin.display(description='arm64')
    def ARM64(self, obj):
        return str(obj.arm64).split('/')[-1]
        # return obj.arm64

    @admin.display(description='amd64')
    def AMD64(self, obj):
        return str(obj.amd64).split('/')[-1]
        # return obj.amd64

    #
    @receiver(pre_delete, sender=versioncontrol)
    def handle_camera_deleted(instance: versioncontrol, **_):
        instance.arm64.delete(save=False)
        instance.amd64.delete(save=False)


    @admin.display(description='操作')
    def Operate(self, obj):
        if obj.stats == 's':
            format_td = format_html('''
            <a style="padding:2px;" href="/admin/FmyTest/versioncontrol/{rid}/change/">编辑</a>&nbsp;
            <a style="padding:2px;cursor:pointer;color:#409eff" onclick="statscontrol({s_s})">禁用</a>
            ''', rid=obj.id, s_s=obj.id)
        elif obj.stats == 'f':
            format_td = format_html('''
            <a style="padding:2px;" href="/admin/FmyTest/versioncontrol/{rid}/change/">编辑</a>&nbsp;
            <a style="padding:2px;cursor:pointer;color:#409eff" onclick="statscontrol({f_s})">启用</a>
            ''', rid=obj.id, f_s=obj.id)
        return format_td

    @receiver(pre_save, sender=versioncontrol)
    def file_update(sender, **kwargs):
        upload_folder_instance = kwargs['instance']
        if upload_folder_instance.id:
            try:
                lst = [upload_folder_instance.arm64.path,upload_folder_instance.amd64.path]
                for i in lst:
                    directory, filename = os.path.split(i)
                    if "uploadss" not in directory:
                        re_path = directory + '\\uploadss\\' + filename
                        print(re_path,type(re_path),'hhh')
                        os.remove(re_path)
            except Exception as ex:
                print(ex,'ex')


    # # Operate.short_description = '操作'
    class Media:
        js = ('js/version-func.js',)
        # css = ('css/version-func.css',)


    # def has_add_permission(self, request):
    #     # 禁用添加按钮
    #     return True
